/*
 * vim:et sts=4 sw=4 cindent:
 * $Id$
 */

import com.digg.services.API;
import com.digg.services.Request;
import com.digg.fdk.model.Story;
import com.digg.services.response.StoriesResponse;
import in4arch.alg.treemap.*;

API.appKey = 'http://diggflashdevkit.googlecode.com/svn/trunk/as2/examples/treemap';

// get the most recent story from the API
var response:StoriesResponse = API.getRecentStories(100, API.STORIES_POPULAR);
response.addLoadListener(onStoriesLoaded);

function onStoriesLoaded(event:Object):Void
{
    if (response.isLoaded())
    {
        var stories:Array = response.getItems();
        var max_comments:Number = 0;
        var max_diggs:Number = 0;
        var tree:Object = new Object();
        tree.containers = new Object();
        tree.container_names = new Array();
        for (var i:Number = 0; i < stories.length; i++)
        {
            var story:Story = stories[i];
            trace(story.diggs + '\t' + story.topic + '\t' + story.container);
            if (!tree.containers[story.container.name])
            {
                tree.containers[story.container.name] = new Object();
                tree.containers[story.container.name].name = story.container.name;
                tree.container_names.push(story.container.name);
                tree.containers[story.container.name].diggs = 0;
                tree.containers[story.container.name].comments = 0;
                tree.containers[story.container.name].topics = new Object();
                tree.containers[story.container.name].topic_names = new Array();
            }
            var container:Object = tree.containers[story.container.name];
            container.diggs = container.diggs + story.diggs;
            container.comments = container.comments + story.comments;
            if (!container.topics[story.topic.name])
            {
                //trace('adding ' + story.topic + ' to ' + container.name);
                container.topics[story.topic.name] = new Object();
                container.topics[story.topic.name].name = story.topic.name;
                container.topic_names.push(story.topic.name);
                container.topics[story.topic.name].diggs = 0;
                container.topics[story.topic.name].comments = 0;
                container.topics[story.topic.name].stories = new Array();
            }
            var topic:Object = container.topics[story.topic.name];
            topic.diggs = topic.diggs + story.diggs;
            topic.comments = topic.comments + story.comments;
            topic.stories.push(story);
            max_comments = Math.max(max_comments,story.comments);
            max_diggs = Math.max(max_diggs,story.diggs);
        }

        var container_items:Array = new Array();
        var order = 0;
        for (var i = 0; i < tree.container_names.length; i++) {
            var container:Object = tree.containers[tree.container_names[i]];
            trace(container.name + ':' + container.diggs);
            container_items.push(new MapItem(container.diggs, i));
        }
        
        /*
         * Prepare main rectangle (canvas for the treemap, we assume that 
         * the whole stage gets used for it), than wrap items array from the
         * previous step with in4arch.alg.treemap.MapModel.
         * Then choose appropriate layout rendering algorithm and
         * process layout.
         */
        var rect:Rect = new Rect(0, 0, Stage.width, Stage.height);
        //treemap canvas
        var model:MapModel = new SimpleMapModel(container_items);
        //wrap items with MapModel
        var alg:MapLayout = new PivotBySplitSize();
        //tried PivotBySplitSize layout algorithm
        alg.layout(model, rect);
        //do layout
        
        assert(container_items.length == container_names.length);

        /*
         * Once again iterate through items array, and render each item.
         */

        var emphatic:TextFormat = new TextFormat();
        emphatic.bold = true;
        emphatic.size = 8;
        emphatic.font = "Helvetica";

        var stories_drawn = 0;
        for (var i = 0; i < container_items.length; i++)
        {
            var container:Object = tree.containers[tree.container_names[i]];
            var container_item:MapItem = container_items[i];
            var container_rect:Rect = container_item.getRect();
            //trace(container_item.getSize() + " == " + container.diggs);
            
            _root.moveTo(container_rect.x, container_rect.y);
            _root.beginFill(0xffff00);
            //random fill alpha just to see that there are squares
            _root.lineTo(container_rect.x+container_rect.w, container_rect.y);
            _root.lineTo(container_rect.x+container_rect.w, container_rect.y+container_rect.h);
            _root.lineTo(container_rect.x, container_rect.y+container_rect.h);
            _root.endFill();
            var txt:TextField = _root.createTextField(container.name, _root.getNextHighestDepth(), container_rect.x, container_rect.y, container_rect.w, container_rect.h);
            txt.text = i + ' ' + container.name + ' ' + container.diggs;
            //trace(i + ' ' + container.name + ' ' + container.diggs);
            txt.setTextFormat(emphatic);
            trace(container.name + "\t" +(container_rect.w*container_rect.h) + "\t" + container.diggs);
    
            var topic_items:Array = new Array();
            var container:Object = tree.containers[tree.container_names[i]];
            //trace(tree.container_names[i] + ': ' + container);
            for (var j:String in container.topics)
            {
                //trace("blah" + container.topics[j].name + ':' + container.topics[j].diggs);
                topic_items.push(new MapItem(container.topics[j].diggs,2));
            }
            assert(topic_items.length == container.topic_names.length);

            var padded_rect:Rect = new Rect(container_rect.x+2,container_rect.y+2,container_rect.w-4,container_rect.h-4);
            
            var topic_model:MapModel = new SimpleMapModel(topic_items);
            alg.layout(topic_model, padded_rect);

            for (var j:Number = 0; j < topic_items.length; j++)
            {
                var topic_item:MapItem = topic_items[j];
                var topic_rect:Rect = topic_item.getRect();

                _root.moveTo(topic_rect.x, topic_rect.y);
                _root.beginFill(0xff00ff);
                //random fill alpha just to see that there are squares
                _root.lineTo(topic_rect.x+topic_rect.w, topic_rect.y);
                _root.lineTo(topic_rect.x+topic_rect.w, topic_rect.y+topic_rect.h);
                _root.lineTo(topic_rect.x, topic_rect.y+topic_rect.h);
                _root.endFill();
                //var txt:TextField = _root.createTextField('txt'+j, _root.getNextHighestDepth(), topic_rect.x, topic_rect.y, topic_rect.w, topic_rect.h);
                //txt.text = topic.diggs; // + ' ' + story.title;
                //txt.setTextFormat(emphatic);
                //trace(rect.w*rect.h/topic.diggs);

                
                var story_items:Array = new Array();
                var topic:Object = container.topics[container.topic_names[j]];
                //trace(container.topic_names[j] + ': ' + topic);
                for (var k:String in topic.stories)
                {
                    //trace(topic.stories[k].title + ':' + topic.stories[k].diggs);
                    story_items.push(new MapItem(topic.stories[k].diggs, 3));
                }
                //trace (story_items.length);

                var padded_rect_t:Rect = new Rect(topic_rect.x+2,topic_rect.y+2,topic_rect.w-4,topic_rect.h-4);
            
                var story_model:MapModel = new SimpleMapModel(story_items);
                alg.layout(story_model, padded_rect_t);

                for (var k:Number = 0; k < story_items.length; k++)
                {
                    var story_item:MapItem = story_items[k];
                    var story_rect:Rect = story_item.getRect();
                    var story:Story = topic.stories[k];
                    
                    //trace(i+' '+story_rect);
                    //draw squares
                    //var sq:MovieClip = _root.createEmptyMovieClip('square_'+i, this.getNextHighestDepth());
                    //trace(sq);
                    _root.moveTo(story_rect.x, story_rect.y);
                    _root.beginFill(0xffffff,100*story.comments/max_comments);
                    //random fill alpha just to see that there are squares
                    _root.lineTo(story_rect.x+story_rect.w, story_rect.y);
                    _root.lineTo(story_rect.x+story_rect.w, story_rect.y+story_rect.h);
                    _root.lineTo(story_rect.x, story_rect.y+story_rect.h);
                    _root.endFill();
                    //var txt:TextField = _root.createTextField('txt'+k, _root.getNextHighestDepth(), story_rect.x, story_rect.y, story_rect.w, story_rect.h);
                    //txt.text = ""+story.diggs; // + ' ' + story.title;
                    //txt.setTextFormat(emphatic);
                    //trace(rect.w*rect.h/story.diggs);
                    stories_drawn++;
                }
            }
        }
        trace(stories_drawn);
    } else {
        field.text = response.getError();
    }
    
}

//random helper
function randRange(min:Number, max:Number):Number {
    var randomNum:Number = Math.round(Math.random()*(max-min))+min;
    return randomNum;
}
